package netty;

import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.util.NettyRuntime;
import org.junit.Test;

/**
 * Netty相关源码阅读入口类
 *
 * @author ala
 * @date 2025-04-03 11:24
 */
public class NettyStudy {



    @Test
    public void testServerBootstrap() throws InterruptedException {
        ServerBootstrap boot = new ServerBootstrap();

        //  默认cpu核数 * 2
        EventLoopGroup bosser = new NioEventLoopGroup();
        //  cpu核数 * 8
        EventLoopGroup worker = new NioEventLoopGroup(NettyRuntime.availableProcessors() * 8);
        boot.group(bosser, worker)
                .channel(NioServerSocketChannel.class)
                .childOption(ChannelOption.SO_KEEPALIVE, true)
                .childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000)
                .childOption(ChannelOption.SO_BACKLOG, 128)
                .childOption(ChannelOption.SO_SNDBUF, 1024)
                .childOption(ChannelOption.SO_RCVBUF, 1024)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                    }
                });
        IdleStateHandler d;


        Channel mainChannel = boot.bind(51001).sync().channel();
        System.out.println(mainChannel.isActive());
        mainChannel.close().sync();
        System.out.println(mainChannel.isActive());
    }

}
